package cn.e2win.biz.cyzj;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.e2win.base.controller.MyException;
import cn.e2win.base.entity.BaseEntity;
import cn.e2win.base.entity.SysAttach;
import cn.e2win.base.entity.SysLabel;
import cn.e2win.base.json.vo.Pager;
import cn.e2win.biz.base.BaseDAO;
import cn.e2win.biz.base.BaseService;
import cn.e2win.biz.base.CommonService;
import cn.e2win.biz.dbo.CyzjEmp;
import cn.e2win.biz.dbo.CyzjOrg;
import cn.e2win.biz.dbo.DsRelation;


@Service
public class CyzjService extends BaseService{
	

	@Autowired
	private CyzjDao cyzjDao;
	@Autowired
	private CommonService commonService;
	
	@Override
	public BaseDAO initDao() {
		return cyzjDao;
	}

	public void pagerCyzj(Pager pager){
		cyzjDao.pagerCyzj(pager);
	}


	public void pagerDsRelatedCyzj(Pager pager){
		cyzjDao.pagerDsRelatedCyzj(pager);
	}


	@Transactional
	public CyzjOrg editCyzj(CyzjOrg obj, List<DsRelation> dsList) throws MyException {
		//保存主图片

		if(obj.getMainPic()!=null && obj.getMainPic().getSize()>0){
			SysAttach attach = uploadFile(obj.getMainPic());
			obj.setMainPicUrl(attach.getFilePath());
		}
		//保存logo

		if(obj.getLogo()!=null && obj.getLogo().getSize()>0){
			SysAttach logo = uploadFile(obj.getLogo());
			obj.setLogoUrl(logo.getFilePath());
		}
		CyzjOrg cyzjOrg = (CyzjOrg)super.edit(obj);
		
		//更新专管员
		cyzjDao.updateAdmin(cyzjOrg);
		
		//保存轮播图
		SysAttach attachTpl = new SysAttach(cyzjOrg,"picLoop");
		super.saveAll(uploadFile(obj.getPicLoopFile() , attachTpl));

		//保存导师列表
		commonService.saveDsRelationList(cyzjOrg, dsList);

		
		SysLabel labelTpl = new SysLabel(cyzjOrg, "labelMain");
		commonService.editLabel(obj.getLabelMain(), labelTpl);
		

		labelTpl.setLabelType("labelOffice");
		commonService.editLabel(obj.getLabelOffice(), labelTpl);
		return cyzjOrg;
		
		
	}

	@Transactional
	public CyzjEmp editCyzjEmp(CyzjEmp obj) throws MyException {
		//新添加用户
		if(obj.getId()==null){
			if(commonService.getEmpByMobile(obj.getMobile()).size()!=0){
				throw new MyException("当前用户的手机号【" + obj.getMobile() + "】已注册，请检查后重试！");
			}
		}else{
			obj.setMobile(null);
		}
		
		//Logo
		if(obj.getIcon()!=null && obj.getIcon().getSize()>0){
			SysAttach attach = uploadFile(obj.getIcon());
			obj.setIconUrl(attach.getFilePath());
		}
		
		CyzjEmp emp = (CyzjEmp)super.edit(obj);//保存用户
		if(obj.getId()==null){
			commonService.saveUser4Emp(emp);
		}
		return emp;
	}

	public void pagerCyzjEmp(Pager pager) {
		cyzjDao.pagerCyzjEmp(pager);
	}
	public Map<String, Object> getCyzjOrgDetail(Integer id, Map<String, String> param){
		return cyzjDao.getCyzjOrgDetail(id, param);
	}


}

